package com.biz.primus.ms.member.dao.redis;

import com.biz.primus.base.redis.CrudRedisDao;
import com.biz.primus.base.redis.util.RedisUtil;
import com.biz.primus.ms.member.dao.ro.MemberFavouriteRo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 *
 * @author jun.liu
 * @date 2017年10月23日
 * @reviewer
 * @see
 */
@Repository
public class MemberFavouriteRedisDao extends CrudRedisDao<MemberFavouriteRo, Long>
{

    private String memberIdAndProductCodeMappingKey(Long memberId, String productCode)
    {
        return getKeyByParams("memberId", memberId, "productCode", productCode);
    }

    /**
     * 保存
     */
    @Override
    public void save(MemberFavouriteRo ro)
    {
        super.save(ro);
        if (ro.getMemberId() != null && ro.getMemberId() != 0L)
        {
            if (StringUtils.isNotBlank(ro.getProductCode()))
            {
                set(this.memberIdAndProductCodeMappingKey(ro.getMemberId(), ro.getProductCode()), RedisUtil.toByteArray(ro.getId()));

            }
            String key = this.getKeyByParams("memberId", ro.getMemberId());
            //已删除 没有在收藏里面了
            if (ro.getDeleteFlag())
            {
                zrem(key, RedisUtil.toByteArray(ro.getId()));
            }//未删除 还在收藏里面
            else
            {
                zadd(key, ro.getCreateTimestamp().getTime(), RedisUtil.toByteArray(ro.getMemberId()));
            }
        }

    }

    public MemberFavouriteRo findByMemberIdAndProductCode(Long memberId, String productCode)
    {
        if (memberId != null && memberId != 0L && StringUtils.isNotBlank(productCode))
        {
            byte[] bs = super.get(this.memberIdAndProductCodeMappingKey(memberId, productCode));
            if (bs == null)
            {
                return null;
            }
            Long id = RedisUtil.byteArrayToLong(bs);
            return findOne(id);
        }
        else
        {
            return null;
        }
    }

    public void deleteByMemberId(Long memberId)
    {
        String key = this.getKeyByParams("memberId", memberId);
        Set<byte[]> bytes = zRange(key, 0, -1);
        if (CollectionUtils.isNotEmpty(bytes))
        {
            List<Long> ids = RedisUtil.bytesSetToLongList(bytes);
            List<MemberFavouriteRo> ros = this.findByIds(ids);
            this.delete(memberId, ros);
        }
    }

    public void delete(Long memberId, List<MemberFavouriteRo> favoriteProductRos)
    {
        String key = this.getKeyByParams("memberId", memberId);
        for (MemberFavouriteRo ro : favoriteProductRos)
        {
            super.delete(ro);
            zrem(key, RedisUtil.toByteArray(ro.getId()));
        }
    }

    public List<MemberFavouriteRo> findPageMemberFavProducts(Long memberId, Integer page, Integer size)
    {
        String key = this.getKeyByParams("memberId", memberId);
        //0
        long start = page * size;
        //10
        long end = (page + 1) * size - 1;
        Set<byte[]> set = zrevrange(key, start, end);
        if (CollectionUtils.isNotEmpty(set))
        {
            List<Long> ids = RedisUtil.bytesSetToLongList(set);
            return super.findByIds(ids);
        }
        return Lists.newArrayList();
    }

    public Long getFavoriteIdsTotals(Long memberId)
    {
        String key = this.getKeyByParams("memberId", memberId);
        Set<byte[]> set = zrevrange(key, 0, -1);
        if (CollectionUtils.isNotEmpty(set))
        {
            return (long)set.size();
        }
        return 0L;
    }

    public List<Long> getFavoriteIds(Long memberId)
    {
        String key = this.getKeyByParams("memberId", memberId);
        Set<byte[]> bytes = zRange(key, 0, -1);
        if (CollectionUtils.isNotEmpty(bytes))
        {
            return RedisUtil.bytesSetToLongList(bytes);
        }
        return Lists.newArrayList();
    }
}